package com.gzhryc.shared_device.oem.code.config.services;

import com.gzhryc.common.StringTools;
import com.gzhryc.common.exceptions.LogicException;
import com.gzhryc.common.logging.Logger;
import com.gzhryc.jdbc.Conditions;
import com.gzhryc.jdbc.MultiDBTools;
import com.gzhryc.shared_device.common.payment.dao.db.AlipayConfig;
import com.gzhryc.shared_device.common.payment.dao.db.WXPayConfig;
import com.gzhryc.shared_device.common.payment.services.AlipayConfigService;
import com.gzhryc.shared_device.common.payment.services.WXPayConfigService;
import com.gzhryc.shared_device.oem.code.config.dao.AgreementDocumentDao;
import com.gzhryc.shared_device.oem.code.config.dao.db.AgreementDocument;
import com.gzhryc.shared_device.oem.code.config.dao.enums.AgreementDocumentEnum;
import com.gzhryc.shared_device.oem.code.config.dao.enums.EAgreementCode;

import java.sql.SQLException;
import java.util.Date;
import java.util.List;

public class AgreementDocumentService {

    static Logger log = Logger.getLogger(AgreementDocumentService.class);

    public static AgreementDocumentService self() {
        return MultiDBTools.getService(AgreementDocumentService.class, true);
    }

    public static AgreementDocumentService self(String jdbcKey) {
        return MultiDBTools.getService(jdbcKey, AgreementDocumentService.class, true);
    }

    AgreementDocumentDao dao;

    public AgreementDocumentService(String jdbcKey) {
        this.dao = new AgreementDocumentDao(jdbcKey);
    }

    public List<AgreementDocument> findAll(){
        try {
            return dao.findAll();
        } catch (SQLException e) {
            log.error(e.getMessage(),e);
        }
        return null;
    }

    public AgreementDocument getById(Long id){
        try {
            return dao.getById(id);
        } catch (SQLException e) {
            log.info(e.getMessage(),e);
        }
        return null;
    }

    public AgreementDocument getByKeys(String code,String appId){
        if(StringTools.isNotBlank(code) && StringTools.isNotBlank(appId)) {
            Conditions conditions = new Conditions(AgreementDocument.class);
            conditions.and().is("code",code);
            conditions.and().is("appId",appId);

            try {
                return dao.get(conditions);
            } catch (SQLException e) {
                log.info(e.getMessage(), e);
            }
        }
        return null;
    }

    public boolean add(AgreementDocument agreementDocument) throws LogicException {
        if(StringTools.isNotBlank(agreementDocument.getCode()) && StringTools.isNotBlank(agreementDocument.getContent())
            && StringTools.isNotBlank(agreementDocument.getAppId())){
            EAgreementCode agreementCode = EAgreementCode.valueOf(agreementDocument.getCode());
            if (agreementCode == null) {
                throw new LogicException("未找到协议类型");
            }
            AgreementDocument temp = getByKeys(agreementDocument.getCode(),agreementDocument.getAppId());
            if(temp != null){
                throw new LogicException("协议已存在");
            }
            WXPayConfig wxPayConfig = WXPayConfigService.self().getByAppId(agreementDocument.getAppId());
            if(wxPayConfig != null){
                agreementDocument.setAppType(AgreementDocumentEnum.AppType.Weixin.index());
            }else {
                AlipayConfig alipayConfig = AlipayConfigService.self().getByAppId(agreementDocument.getAppId());
                if(alipayConfig != null){
                    agreementDocument.setAppType(AgreementDocumentEnum.AppType.Aliyun.index());
                }else{
                    throw new LogicException("无效的APPID");
                }
            }

            if(StringTools.isBlank(agreementDocument.getTitle())){
                agreementDocument.setTitle(agreementCode.note());
            }
            agreementDocument.setCreateDate(new Date());
            try {
                return dao.insert(agreementDocument) > 0;
            } catch (SQLException e) {
                log.info(e.getMessage(),e);
            }
        }
        return false;
    }

    public boolean updateNotNull(AgreementDocument agreementDocument) throws LogicException {
        if (StringTools.isNotBlank(agreementDocument.getCode())) {
            EAgreementCode agreementCode = EAgreementCode.valueOf(agreementDocument.getCode());
            if (agreementCode == null) {
                throw new LogicException("未找到协议类型");
            }
        }

        if (StringTools.isNotBlank(agreementDocument.getAppId())) {
            WXPayConfig wxPayConfig = WXPayConfigService.self().getByAppId(agreementDocument.getAppId());
            if (wxPayConfig != null) {
                agreementDocument.setAppType(AgreementDocumentEnum.AppType.Weixin.index());
            } else {
                AlipayConfig alipayConfig = AlipayConfigService.self().getByAppId(agreementDocument.getAppId());
                if (alipayConfig != null) {
                    agreementDocument.setAppType(AgreementDocumentEnum.AppType.Aliyun.index());
                } else {
                    throw new LogicException("无效的APPID");
                }
            }
        }

        try {
            return dao.updateNotNull(agreementDocument) > 0;
        } catch (SQLException e) {
            log.info(e.getMessage(), e);
        }

        return false;
    }

    public boolean deleteById(Long id) throws LogicException {
        try {
            return dao.deleteById(id) > 0;
        } catch (SQLException e) {
            log.info(e.getMessage(),e);
        }
        return false;
    }
}
